package com.task;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.http.ApacheHttpUtil;
import com.http.JavaHttpUtil;

/**
 * 定时任务<br>
 * JAVA实现定时任务的几种方式<br>
 * JDK 自带的定时器实现 <br>
 * Quartz 定时器实现<br>
 * Spring 相关的任务调度
 * 
 * @author zincredible
 * @date 2019/04/11 16:10:18
 */
public class TimeTask {

	public static void main(String[] args) throws InterruptedException {

		ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();

		Runnable runnable = () -> {
			String content = JavaHttpUtil.get("http://www.weather.com.cn/data/sk/101110101.html");
			// String content =
			// ApacheHttpUtil.get("http://www.weather.com.cn/data/sk/101110101.html");
			System.out.println(content);
		};

		// 每2s执行一次任务
		Future<?> future=scheduledThreadPool.scheduleAtFixedRate(runnable, 0, 2, TimeUnit.SECONDS);
		// 5秒后停止定时任务
		scheduledThreadPool.shutdown();
	}
}
